import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-06-27
 * Time: 22:10
 */
public class test1 {
    public boolean isValid(String s) {
        int n = s.length();
        Stack<Character> stack = new Stack<Character>();
        for(int i = 0;i<n;i++){
            char ch = s.charAt(i);
            // 左括号的情况
            if(ch=='(' || ch=='[' || ch=='{'){
                stack.push(ch);
            }else{
                // 右括号的情况
                if(stack.isEmpty()){
                    return false;
                }
                char currentCh = stack.pop();
                if( (ch==')' && currentCh!='(') || (ch==']' && currentCh!='[') || (ch=='}' && currentCh!='{') ){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
